package com.example.wang.myapplication;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.liqi.nohttputils.RxNoHttpUtils;
import com.liqi.nohttputils.interfa.DialogGetListener;
import com.liqi.nohttputils.nohttp.NoHttpInit;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {

    private Button btn;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main1);
        initView();
        init();
    }

    String url = "https://glaveinfo.com/simulate_pockets/api/web/index.php/v5/basic/version";

    void init() {
//        NoHttp.initialize(this, new NoHttp.Config()
//                .setNetworkExecutor(new OkHttpNetworkExecutor())  // 使用OkHttp做网络层。
//        );
//        NoHttp.initialize(InitializationConfig.newBuilder(this)
//                .networkExecutor(new OkHttpNetworkExecutor())
//                .cacheStore(
//                        // 如果不使用缓存，setEnable(false)禁用。
//                        new DBCacheStore(this).setEnable(true)
//                )
//                .build()
//        );

        RxNoHttpUtils.rxNoHttpInit(getApplicationContext())
                .setRxRequestUtilsWhy(NoHttpInit.OKHTTP)
                .setDbEnable(true)
                .isDebug(true)
                .setDebugName("System.out")
                .setDialogGetListener(new DialogGetListener() {
                    @Override
                    public Dialog getDialog() {
                        return new AlertDialog.Builder(MainActivity1.this).create();
                    }
                })
                .startInit();

        Request<String> stringReq = NoHttp.createStringRequest(url, RequestMethod.POST);
        stringReq.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK);
        Response<String> response = NoHttp.startRequestSync(stringReq);
        if (response == null) {
            System.out.println("--");
            return;
        }
        System.out.println(response.responseCode());
        System.out.println(response.get() + "");
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(this);
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                RxNoHttpUtils.rxNohttpRequest()
                        .post()
                        .url(url)
                        .setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK)
                        .builder(String.class, new OnMyIsRequestListener<DataResult<UpdateInfo>>() {
                            @Override
                            public void onError(Throwable throwable) {

                            }

                            @Override
                            public void onResponse(DataResult<UpdateInfo> result) {
                                System.out.println(result.getData().getLastVerCode());
                                System.out.println(result.getData().getAd_up_url());
                                btn1.performClick();
                            }
                        })
                        .requestRxNoHttp();
                break;
            case R.id.btn1:
                startActivity(new Intent(this, MainActivity1.class));
                break;
        }
    }

}
